#!/bin/bash
projectPath=$(cd $(dirname $0) && pwd)
projectName="${projectPath##*/}"
logsPath=${projectPath}/logs
buildPath=${projectPath}/linux_build
installPath=${projectPath}/install

cd ${projectPath}

if test ! -f ${projectPath}/CMakeLists.txt ; then
    echo "can't find CMakeLists.txt"
    exit 0
fi

if test -f "${logsPath}/cmake.log" ; then
    rm ${logsPath}/cmake.log
fi

if test -d ${projectPath} -a ! -d ${logsPath} ; then
    mkidr -p ${logsPath}
fi

mkdir -p ${buildPath}
rm -rf ./build
ln -s ${buildPath} build

cd ${buildPath}
cmake -D PROJECT_NAME=${projectName} -DCMAKE_INSTALL_PREFIX=${installPath} .. | tee ${logsPath}/cmake.log
cpu_num=$(cat /proc/cpuinfo| grep "processor"| wc -l)
make -j${cpu_num}
